import logging
import time

from selenium.common import NoSuchElementException

from base.base_page import BasePage


class CameraPage(BasePage):
    # 允许访问相机权限
    def btn_permission_camera(self):
        return self.by_id("com.android.permissioncontroller:id/permission_allow_foreground_only_button").click()

    # 允许访问媒体权限
    def btn_permission_media(self):
        return self.by_id("com.android.permissioncontroller:id/permission_allow_button").click()

    # 允许访问所有文件
    def btn_permission_file(self):
        return self.by_id("android:id/switch_widget").click()

    # 允许访问所有文件-返回
    def btn_permission_back(self):
        return self.by_accessibility_id("向上导航").click()

    # 立即体验
    def btn_try_it_now(self):
        return self.by_id(self.HW_ENV + "btn_check_for_update").click()

    # 相册导入
    def btn_import_photo(self):
        return self.by_id(self.HW_ENV + "image_open_gallery_left").click()

    # 文档导入
    def btn_import_document(self):
        return self.by_id(self.HW_ENV + "image_open_file").click()

    # 导入
    def btn_import(self):
        return self.by_id(self.HW_ENV + "btn_import").click()

    # 下一步
    def btn_complete(self):
        return self.by_id(self.HW_ENV + "btnComplete").click()

    # 获取权限
    def func_get_permissions(self):
        try:
            self.driver.implicitly_wait(1)
            self.btn_permission_camera()
        except NoSuchElementException:
            pass
        try:
            self.driver.implicitly_wait(1)
            self.btn_permission_media()
        except NoSuchElementException:
            pass
        try:
            self.driver.implicitly_wait(1)
            self.btn_permission_file()
        except NoSuchElementException:
            pass
        try:
            self.driver.implicitly_wait(1)
            self.btn_permission_back()
        except NoSuchElementException:
            pass

    # 关闭立即体验
    def func_tip_close(self):
        try:
            self.driver.implicitly_wait(1)
            self.btn_try_it_now()
        except NoSuchElementException:
            pass

    def func_import_document(self):
        # self.func_import_document_first()
        self.func_import_document_by_name("1_测试集.pdf")

    # 根据文件名导入文档
    def func_import_document_by_name(self, filename):
        try:
            self.driver.implicitly_wait(1)
            self.btn_import_document()
        except NoSuchElementException:
            pass

        time.sleep(1)
        while True:
            if self.is_element(filename):
                self.by_uiautomator('new UiSelector().text("' + filename + '")').click()
                break
            else:
                self.drag(0.5, 0.5, 0.4, 0.1, 800)
        self.btn_import()

        try:
            self.btn_complete()
        except NoSuchElementException:
            pass
        time.sleep(3)

    # 导入第一个文档
    def func_import_document_first(self):
        try:
            self.driver.implicitly_wait(1)
            self.btn_import_document()
        except NoSuchElementException:
            pass
        pdfs = self.by_uiautomators('new UiSelector().resourceId("' + self.HW_ENV + 'item_checkbox")')
        pdfs[0].click()
        self.btn_import()
        try:
            self.btn_complete()
        except NoSuchElementException:
            pass
        time.sleep(3)